<?php
namespace Api\Service;

/**
 * 余额相关
 */
class MoneyService
{
    const PAGE_NUMBER = 10;
    /**
     * 用户余额明细
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function log($user_id, $cur_page = 1)
    {
        $where = ['user_id' => $user_id];

        $count = M('money_log')->where($where)->count();
        if ($count) {
            $page = ceil($count / self::PAGE_NUMBER);
            $data = $res = M('money_log')
                ->field('title,description,money,add_time')
                ->where($where)
                ->page($cur_page, self::PAGE_NUMBER)
                ->order('id desc')
                ->select();
        }

        return [
            'count'    => intval($count),
            'cur_page' => $cur_page,
            'page'     => $page ? $page : 0,
            'data'     => $data ? $data : [],
        ];
    }

    /**
     * 用户余额
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function money($user_id)
    {
        $money = M('user')->where([
            'id' => $user_id,
        ])->getField('money');

        return $money;
    }
}
